<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/7/19
 * Time: 14:13
 */

/**
 * Created by wanglu on 2017/7/19.
 */
class Puser extends AdminBase
{

    function __construct()
    {
        parent::__construct();
        $this->load->service('user_service');
        $this->load->service('project_service');
        $this->load->service('lang_service');
    }


    /**
     * 项目团队成员
     */
    public function user()
    {
        $pid = $this->input->get("pid");
        $project_users = $this->project_service->get_project_users($pid);
       // echo json_encode($project_users);
       // exit;
        $project_user_relation = $this->project_service->get_project_user_relation($pid, $this->loginUser['id']);
        $this->showView("tpl_project_user", array('users' => $project_users, 'project_user_relation' => $project_user_relation));
    }

    /**
     * 添加成员搜索用户
     */
    public function search_project_user()
    {
        $search = $this->input->get("search");
        $pid = $this->input->get("pid");
        $searchusers = $this->project_service->search_not_in_project_users($search, $pid);
        echo json_encode($searchusers);
    }

    /**
     * 添加团队成员
     */
    public function add_project_user()
    {
        $user_id = $this->input->post("user_id");
        $project_id = $this->input->post("project_id");
        $role = $this->input->post("role");
        $lang_permissions = $this->input->post("lang_permissions");
        $lang_permissions = json_decode($lang_permissions, true);
        if ($this->project_service->add_project_user($user_id, $project_id, $role, $lang_permissions)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }

    /**
     * 更新项目团队成员信息
     */
    public function update_project_user() {
        $user_id = $this->input->post("user_id");
        $project_id = $this->input->post("project_id");
        $role = $this->input->post("role");
        $lang_permissions = $this->input->post("lang_permissions");
        $lang_permissions = json_decode($lang_permissions, true);
        if ($this->project_service->update_project_user($user_id, $project_id, $role, $lang_permissions)) {
            die(json_encode(array("code" => 200)));
        }  else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }

    }

    public function delete() {
        $pid = $this->input->post("pid");
        $uid =  $this->input->post("uid");
        if ($this->project_service->remove_project_user($pid, $uid)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }




}